home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / spamexperts / message.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  6.4 KB  |  205 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''message.py
  5.  
  6. Classes:
  7.     SEHeaderMessage - A SBHeaderMessage extended for SpamExperts
  8. '''
  9. import re
  10. import sys
  11. import email
  12. import types
  13. from spambayes.message import SBHeaderMessage
  14. from spambayes.tokenizer import try_to_repair_damaged_base64
  15. from spamexperts.Options import options
  16.  
  17. class SEHeaderMessage(SBHeaderMessage):
  18.     '''Message class that is cognizant of SpamExperts headers and the
  19.     Spambayes headers.'''
  20.     
  21.     def __init__(self, ID = None):
  22.         SBHeaderMessage.__init__(self, ID)
  23.         self.stored_attributes = [
  24.             'c',
  25.             't',
  26.             'block_state',
  27.             'account',
  28.             'date_modified',
  29.             'internaldate',
  30.             'flags',
  31.             'folder_name',
  32.             'uid']
  33.         self.uid = None
  34.         self.flags = None
  35.         self.account = None
  36.         self.folder_name = None
  37.         self.block_state = None
  38.         self.internaldate = None
  39.  
  40.     
  41.     def setBlockState(self, state):
  42.         '''Store the block state.'''
  43.         self.block_state = state
  44.         self.modified()
  45.  
  46.     
  47.     def setAccount(self, account):
  48.         '''Store the account this message belongs to for moving between
  49.         blocked/delayed databases.'''
  50.         self.account = account
  51.         self.modified()
  52.  
  53.     
  54.     def rememberBlockingState(self, account, state):
  55.         '''Store the blocking state for this message.'''
  56.         self.setAccount(account)
  57.         self.setBlockState(state)
  58.  
  59.     
  60.     def getBlockingState(self):
  61.         '''Get the blocking state for this message.'''
  62.         return (self.account, self.block_state)
  63.  
  64.     
  65.     def setDisposition(self, prob):
  66.         pass
  67.  
  68.     
  69.     def addNotations(self):
  70.         disposition = self.GetTrained()
  71.         if disposition is None:
  72.             disposition = self.GetClassification()
  73.         elif disposition == True:
  74.             disposition = options[('Headers', 'header_spam_string')]
  75.         elif disposition == False:
  76.             disposition = options[('Headers', 'header_ham_string')]
  77.         
  78.         self.notateTo(disposition)
  79.         self.notateSubject(disposition)
  80.  
  81.     
  82.     def notateSubject(self, disposition):
  83.         '''Add the appropriate string to the subject: and/or to: header.'''
  84.         if isinstance(options[('Headers', 'notate_subject')], types.StringTypes):
  85.             notate_subject = (options[('Headers', 'notate_subject')],)
  86.         else:
  87.             notate_subject = options[('Headers', 'notate_subject')]
  88.         if disposition in notate_subject:
  89.             
  90.             try:
  91.                 self.replace_header('Subject', '[%s] %s' % (disposition, self['Subject']))
  92.             except KeyError:
  93.                 self['Subject'] = '[%s]' % (disposition,)
  94.             except:
  95.                 None<EXCEPTION MATCH>KeyError
  96.             
  97.  
  98.         None<EXCEPTION MATCH>KeyError
  99.  
  100.     
  101.     def delNotations(self):
  102.         '''If present, remove our notation from the subject: and/or to:
  103.         header of the message.'''
  104.         subject = self['Subject']
  105.         if not subject:
  106.             SBHeaderMessage.delNotations(self)
  107.             return None
  108.         
  109.         self.replace_header('Subject', '')
  110.         SBHeaderMessage.delNotations(self)
  111.         if subject and options[('Headers', 'notate_subject')]:
  112.             ham = '[%s] ' % (options[('Headers', 'header_ham_string')],)
  113.             spam = '[%s] ' % (options[('Headers', 'header_spam_string')],)
  114.             unsure = '[%s] ' % (options[('Headers', 'header_unsure_string')],)
  115.             for disp in (ham, spam, unsure):
  116.                 if subject.startswith(disp):
  117.                     self.replace_header('Subject', subject[len(disp):])
  118.                     break
  119.                     continue
  120.             
  121.         
  122.  
  123.     
  124.     def addHeaders(self, prob, clues):
  125.         self.addSBHeaders(prob, clues)
  126.  
  127.     
  128.     def currentHeaders(self):
  129.         '''Return all current headers.'''
  130.         return self.currentSBHeaders()
  131.  
  132.     
  133.     def delHeaders(self):
  134.         '''Remove all custom headers from this message.'''
  135.         self.delSBHeaders()
  136.  
  137.     
  138.     def _walkMultiPart(self, msg, content_type):
  139.         '''"Return the first part of the specified type.'''
  140.         for part in msg.get_payload():
  141.             if part.is_multipart():
  142.                 return self._walkMultiPart(part, content_type)
  143.             
  144.             t = part.get_content_type()
  145.             if t == content_type:
  146.                 
  147.                 try:
  148.                     if not part.get_content_charset():
  149.                         pass
  150.                     return unicode(part.get_payload(decode = True), 'latin-1')
  151.                 except (LookupError, UnicodeDecodeError):
  152.                     return unicode(part.get_payload(decode = True), 'latin-1')
  153.                 except:
  154.                     None<EXCEPTION MATCH>(LookupError, UnicodeDecodeError)
  155.                 
  156.  
  157.             None<EXCEPTION MATCH>(LookupError, UnicodeDecodeError)
  158.         
  159.         return ''
  160.  
  161.     strip_tags_re = re.compile('<.*?>', re.IGNORECASE | re.DOTALL)
  162.     
  163.     def _strip_html(self, content):
  164.         '''Remove all HTML tags from the content, to produce a simple
  165.         plain-text version.'''
  166.         return self.strip_tags_re.sub('', content)
  167.  
  168.     
  169.     def as_html(self):
  170.         '''Return the first text/plain or text/html part of the
  171.         message, converted to HTML.'''
  172.         if self.is_multipart():
  173.             body = self._walkMultiPart(self, 'text/plain')
  174.             if body == '':
  175.                 body = self._strip_html(self._walkMultiPart(self, 'text/html'))
  176.             
  177.         else:
  178.             body = self.get_payload(decode = True)
  179.             
  180.             try:
  181.                 if not self.get_content_charset():
  182.                     pass
  183.                 body = unicode(body, 'latin-1')
  184.             except (UnicodeDecodeError, LookupError):
  185.                 body = unicode(body, 'latin-1')
  186.  
  187.         
  188.         try:
  189.             body = try_to_repair_damaged_base64(body)
  190.         except UnicodeDecodeError:
  191.             pass
  192.  
  193.         return body
  194.  
  195.     
  196.     def fingerprint_hash(self):
  197.         '''Return an ID suitable for use with the fingerprint system.'''
  198.         return hash(self)
  199.  
  200.  
  201.  
  202. def seheadermessage_from_string(s, _class = SEHeaderMessage, strict = False):
  203.     return email.message_from_string(s, _class)
  204.  
  205.